// 数组的方法

// es6 find  findIndex

// reduce 
// 一列对象的求和
let arr = [{ name: 'xx', price: 10 }, { name: 'xx', price: 20 }, { name: 'xx', price: 30 }]

let total = arr.reduce((pre, item) => {
  return pre + item.price
}, 0)

console.log(total)

//  return  装逼写法
// (表达式, return 的值)

// 几个函数组合起来 
function sum(a, b) {
  console.log('sum',a, b)
  return a + b
}
function up(str) {
  console.log('up', str)
  return str.toUpperCase()
}
function add(str) {
  console.log('add', str)
  return '***' + str + '**'
}
function compose(...funcs) {
  return funcs.reduce((a, b) => (...args) => a(b(...args)))
}
let r = compose(add, up, sum)('fbk', 'nb')
console.log(r)

// reduce实现
Array.prototype.reduce = function(cb, pre) {
  if(this.length <= 1) return
  let res
  for(let i =0; i<this.length; i++) {
    // let item = this[i]
    res = pre
    if(res){
      res = cb(res, this[i], i, this)
    }else{
      i+=1 
      res = cb(this[i-1], this[i], i, this)
    }
  }
  return res
}
let r = [1, 2].reduce((pre, item, index, current)=>{
  console.log(pre, item, index)
  return pre+item
})
console.log(r)

// map 方法
